package com.xstore.framework.pagination;

import java.util.Collections;
import java.util.List;

@SuppressWarnings("unchecked")
public class Pagination<T> {

	public final static int	PAGESIZE	= 15;
	/**
	 * 每页个数
	 */
	private int	pageSize = PAGESIZE;

	/**
	 * 分页数据
	 */
	private List items = Collections.EMPTY_LIST;

	/**
	 * 总结果数
	 */
	private int	nrOfElements;

	/**
	 * 分页索引的数组
	 */
	private int[] indexes = new int[0];

	/**
	 * 当前页索引
	 */
	private int	startIndex	= 0;

	public Pagination(List items, int totalCount) {
		setPageSize(PAGESIZE);
		setNrOfElements(totalCount);
		setItems(items);
		setStartIndex(0);
	}

	public Pagination(List items, int totalCount, int startIndex) {
		setPageSize(PAGESIZE);
		setNrOfElements(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	public Pagination(List items, int totalCount, int startIndex, int pageSize) {
		setPageSize(pageSize);
		setNrOfElements(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getNrOfElements() {
		return nrOfElements;
	}

	public void setNrOfElements(int totalCount) {
		if (totalCount > 0) {
			this.nrOfElements = totalCount;
			int count = totalCount / pageSize;
			if (totalCount % pageSize > 0) count++;
			indexes = new int[count];
			for (int i = 0; i < count; i++) {
				indexes[i] = pageSize * i;
			}
		} else {
			this.nrOfElements = 0;
		}
	}

	public int[] getIndexes() {
		return indexes;
	}

	public void setIndexes(int[] indexes) {
		this.indexes = indexes;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		if (nrOfElements <= 0) this.startIndex = 0;
		else if (startIndex >= nrOfElements) this.startIndex = indexes[indexes.length - 1];
		else if (startIndex < 0) this.startIndex = 0;
		else {
			this.startIndex = indexes[startIndex / pageSize];
		}
	}

	/**
	 * 首页索引
	 * 
	 * @return
	 */
	public int getFirstIndex() {
		return indexes.length > 0 ? indexes[0] : 0;
	}

	/**
	 * 下一页索引
	 * 
	 * @return
	 */
	public int getNextIndex() {
		int nextIndex = getStartIndex() + pageSize;
		if (nextIndex >= nrOfElements) return getStartIndex();
		else return nextIndex;
	}

	/**
	 * 上一页索引
	 * 
	 * @return
	 */
	public int getPreviousIndex() {
		int previousIndex = getStartIndex() - pageSize;
		if (previousIndex < 0) return 0;
		else return previousIndex;
	}

	/**
	 * 末页索引
	 * 
	 * @return
	 */
	public int getLastIndex() {
		return indexes.length > 0 ? indexes[indexes.length - 1] : 0;
	}

	/**
	 * 是否是末页
	 * 
	 * @return
	 */
	public boolean isLastPage() {
		return !hasNext();
	}

	/**
	 * 是否有下一页
	 * 
	 * @return
	 */
	public boolean hasNext() {
		return getStartIndex() < getLastIndex();
	}

	/**
	 * 是否第一页
	 * 
	 * @return
	 */
	public boolean isFirstPage() {
		return !hasPrevious();
	}

	/**
	 * 是否有上一页
	 * 
	 * @return
	 */
	public boolean hasPrevious() {
		return getStartIndex() > getFirstIndex();
	}

	/**
	 * 共多少页
	 * 
	 * @return
	 */
	public int getPageCount() {
		return getIndexes().length;
	}

	public int getPage() {
		for (int i = 0; i < getIndexes().length; i++) {
			int sI = getIndexes()[i];
			if (sI == getStartIndex()) { return i + 1; }
		}
		return 1;
	}
}
